var login = {
	load : function(){
		$.get("/dotcao/ws/users/loggedin", function(data){
			if(data.firstname){
				$("#auth-displayname").html(data.firstname);
			}
		}, "json");
		
	},
		
	create : function() {
		// Expand Panel
		$("#open").click(function() {
			$("div#panel").slideDown("slow");
		});
		
		// Collapse Panel
		$("#close").click(function() {
			$("div#panel").slideUp("slow");
		});
		
		// Switch buttons from "Log In | Register" to "Close Panel" on click
		$("#toggle a").click(function() {
			$("#toggle a").toggle();
		});
		
		$("#login-send").bind("click", function(){
			var _username = $("#login-username").val();
			var _password = $("#login-password").val();
			
			if(_username && _password){
				$.post("/dotcao/ws/users/auth", {username: _username, password: _password}, function(data){
					$("<div />").html(data.content).appendTo("#content").dialog({
						title : "Autenticação",
						show : "fade",
						hide : "fade",
						modal: true,
						width : 300,
						height : 150,
						buttons : {
							OK : function() {
								if(data.type == "SUCESSO"){
									window.location = "wall.html";
								}
								
								$(this).dialog("close");
							}
						}
					});
				});
			}
		});
		
		$("#btn-register").bind("click", function() {
			var email = $("#register-email").val();
			var name = $("#register-name").val();
			
			if (email) {
				$.get("/dotcao/ws/users/register/" + email + "/" + name, function(data) {
					$("<div />").html(data.content).appendTo("#content").dialog({
						title : "Solicitação de Registro",
						show : "fade",
						hide : "fade",
						width : 300,
						height : 150,
						buttons : {
							OK : function() {
								$(this).dialog("close");
							}
						}
					});
				}, "json");
			}
			
			return false;
		});
		
		this.load();
	}
};

